//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 

package org.trafficman.trafficman_main;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.Menu.Item;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Gallery.LayoutParams;

public class TrafficmanFilenameNotepad extends ListActivity {
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;

	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;

	private TrafficmanFilesNotesDbAdapter mDbHelper;
	private int int_intent_active;
	private String title_text;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Window w = getWindow();
		w.requestFeature(Window.FEATURE_LEFT_ICON);

		setContentView(R.layout.files_notes_list_edit);

		LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
		TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
		TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
		get_JobView_Screen(); // set JobView2 background
		int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
				null);
		tmpLayout.setBackground(resID);


		mDbHelper = new TrafficmanFilesNotesDbAdapter(this);
		mDbHelper.open();
		fillData();

		title_text = getString(R.string.trafficman_file_name_edit);
		this.setTitle(title_text);
		w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,
				R.drawable.tm_icon);
	}

	private void get_JobView_Screen() {
		TrafficmanScreens mScreens = new TrafficmanScreens(this);
	}

	private void fillData() {
		// Get all of the rows from the database and create the item list
		Cursor notesCursor = mDbHelper.fetchAllNotes();
		startManagingCursor(notesCursor);

		// Create an array to specify the fields we want to display in the list
		// (only TITLE)
		String[] from = new String[] { TrafficmanFilesNotesDbAdapter.KEY_JV_TITLE };

		// and an array of the fields we want to bind those fields to (in this
		// case just text1)
		int[] to = new int[] { R.id.text1 };

		// Now create a simple cursor adapter and set it to display
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.files_notes_row, notesCursor, from, to);
		setListAdapter(notes);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, R.string.tm_files_menu_insert,
				R.drawable.roles_04);
		menu.add(0, DELETE_ID, R.string.tm_files_menu_delete,
				R.drawable.roles_05);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, Item item) {
		switch (item.getId()) {
		case INSERT_ID:
			createNote();
			return true;
		case DELETE_ID:
			mDbHelper.deleteNote(getListView().getSelectedItemId());
			fillData();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void createNote() {
		int_intent_active = 1;
		Intent i = new Intent(this, TrafficmanFilesNoteEdit.class);
		startSubActivity(i, ACTIVITY_CREATE);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		int_intent_active = 1;

		int_intent_active = 1;
		TrafficmanConstants.SAVE_CLICK = 1;
		setResult(RESULT_OK);
		
//		Intent i = new Intent(this, JobView_Filename_NoteEdit.class);
//		i.putExtra(JobView_Contacts_NotesDbAdapter.KEY_ROWID, id);
//		startSubActivity(i, ACTIVITY_CREATE);
//		finish();
	}

	private void set_Current_JV_file(long id) {

		TrafficmanFilesNotesDbAdapter selected_JV_File_mDbHelper = new TrafficmanFilesNotesDbAdapter(
				this);
		selected_JV_File_mDbHelper.open();

		Cursor selected_JV_File_Cursor = selected_JV_File_mDbHelper
				.fetchAllNotes();
		startManagingCursor(selected_JV_File_Cursor);
		int record_count = selected_JV_File_Cursor.count();
		if (record_count > 0) {
			selected_JV_File_Cursor.first();
			int columnID = selected_JV_File_Cursor
					.getColumnIndex("jv_selected_file_id");
			for (int i = 0; i < record_count; ++i) {
				selected_JV_File_Cursor.updateInt(columnID, 0);
				selected_JV_File_Cursor.next();
			}
		}
		Cursor update_JV_File_Cursor = selected_JV_File_mDbHelper.fetchNote(id);
		startManagingCursor(update_JV_File_Cursor);
		int record_count2 = update_JV_File_Cursor.count();
		if (record_count2 > 0) {
			update_JV_File_Cursor.first();
			int columnID = update_JV_File_Cursor
					.getColumnIndex("jv_selected_file_id");
			update_JV_File_Cursor.updateInt(columnID, 1);
			update_JV_File_Cursor.commitUpdates();
		}
		
		selected_JV_File_mDbHelper.close();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			String data, Bundle extras) {
		super.onActivityResult(requestCode, resultCode, data, extras);
		fillData();
	}


	public class ImageAdapter extends BaseAdapter {

		private Integer[] mThumbIds = { R.drawable.projects_001,
				R.drawable.tasks_003, R.drawable.schedule_01,
				R.drawable.bidding_001, R.drawable.contacts_01 };

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i = new ImageView(mContext);

			i.setImageResource(mThumbIds[position]);
			i.setAdjustViewBounds(true);
			i.setLayoutParams(new Gallery.LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			i.setBackground(android.R.drawable.picture_frame);
			return i;
		}

		private Context mContext;

	}

	@Override
	protected void onPause() {
		if (int_intent_active == 1) {
			// do nothing
			int_intent_active = 0;
			super.onPause();
		} else {
			super.onPause();
//			Intent intent = new Intent();
//			intent.setClass(JobView_Filename_Notepad.this, JobView_Projects_Notepad.class);
//			startActivity(intent);
//			finish();
		}

	}

}
